<template>
	<view class="container">
		<!-- 弹出消息 -->
		<uv-toast ref="uToast"></uv-toast>
		<!-- 头部导航栏 -->
		<view class="header">
			<view class="nav-back" @click="goBack">
				<uv-icon name="arrow-left" size='32'></uv-icon>
			</view>
			<view class="header-icon">
				<uv-icon name="download" size='28'></uv-icon>
				<uv-icon name="share-square" size='28'></uv-icon>
				<uv-icon name="list" size='28'></uv-icon>
			</view>
		</view>
		<!-- 阅读内容区 -->
		<scroll-view scroll-y class="content-area" :style="contentStyle" @scrolltolower="loadNextChapter">
			<text class="chapter-title">第一章 穿越的唐家三少</text>
			<text class="content-text">{{ chapterContent }}</text>
		</scroll-view>
		<!-- 底部操作栏 -->
		<view class="toolbar">
			<view class="tool" @click="open">
				<uv-icon name="list" size="24" />
				<text>目录</text>
			</view>
			<view class="tool" @click="handleBookshelf">
				<uv-icon :name="isInBookshelf ? 'star-fill' : 'star'" size="24"
					:color="isInBookshelf ? '#f59a23' : '#666'" />
				<text>{{ isInBookshelf ? '已加入' : '加入书架' }}</text>

			</view>
			<view class="tool" @click="settingPopup">
				<uv-icon name="setting" size="24" />
				<text>设置</text>
			</view>
		</view>
		<!-- 目录弹窗 -->
		<view class="chapter-container">
			<uv-popup zIndex="1" ref="popup" mode="bottom" :mask="false" :safe-area-inset-bottom="false"
				border-radius="24" custom-style="bottom: 120rpx; box-shadow: none;">
				<view class="chapter-popup">
					<view class="popup-header">
						<text>连载中 共1220章</text>
					</view>
					<scroll-view scroll-y class="chapter-scroll">
						<view v-for="(chapter, index) in chapters" :key="index" class="chapter-item">
							<text class="title">{{ chapter.title }}</text>
							<view class="status">
								<text v-if="chapter.free" class="free-tag">免费</text>
								<uv-icon v-else name="lock" size="20" color="#999" />
							</view>
						</view>
					</scroll-view>
				</view>
			</uv-popup>
			<!-- 设置弹窗 -->
			<uv-popup ref="settingPopup" mode="bottom" :mask="false" border-radius="24"
				custom-style="bottom: 120rpx; background: #f8f9fa;">
				<view class="setting-popup">
					<!-- 字号设置 -->
					<view class="setting-item">
						<text>字号</text>
						<view class="font-control">
							<text @click="fontSize--" class="btn">A-</text>
							<text class="value">{{ fontSize }}</text>
							<text @click="fontSize++" class="btn">A+</text>
						</view>
					</view>

					<!-- 背景颜色 -->
					<view class="setting-item">
						<text>背景</text>
						<view class="color-picker">
							<view v-for="color in colors" :class="['color', {active: bgColor === color}]"
								:style="{background: color}" @click="bgColor = color">
							</view>
						</view>
					</view>

					<!-- 翻页模式 -->
					<view class="setting-item">
						<text>翻页</text>
						<view class="page-mode">
							<text v-for="mode in modes" :class="['mode-btn', {active: pageMode === mode}]"
								@click="pageMode = mode">
								{{ mode }}
							</text>
						</view>
					</view>
				</view>
			</uv-popup>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isInBookshelf: false,
				// 设置弹窗数据
				fontSize: 21,
				bgColor: '#fff',
				pageMode: '仿真',
				colors: ['#FDF1DC', '#E4F5D8', '#2D2D2D', '#DCE8F5'],
				modes: ['仿真', '上下', '覆盖'],
				//底部操作栏数据
				isOpenList: true,
				isSettingPopup: true,
				// 阅读内容数据
				fontSize: 16,
				bgColor: '#ffffff',
				bgColors: ['#FDF1DC', '#E4F5D8', '#2D2D2D', '#DCE8F5'],
				isBookmarked: false,
				chapterContent: `巴蜀，历来有天府之国的美誉，其中，最有名的门派莫过于唐门...（章节内容）`
			}
		},
		computed: {
			contentStyle() {
				return {
					fontSize: this.fontSize + 'px',
					backgroundColor: this.bgColor
				}
			}
		},
		methods: {
			goBack() {
				uni.navigateBack()
			}, // 目录弹窗打开
			open() {
				if (this.isOpenList) {
					this.$refs.popup.open('bottom');
					this.isOpenList = !this.isOpenList
				} else {
					this.$refs.popup.close();
					this.isOpenList = !this.isOpenList
				}
			},
			// 加入书架
			handleBookshelf() {
				this.isInBookshelf = !this.isInBookshelf
				this.$refs.uToast.show({
					message: this.isInBookshelf ? '已加入书架' : '已移除',
					type: this.isInBookshelf ? 'success' : 'warning',
					duration: 1500
				})
			},
			// 设置弹窗方法
			settingPopup() {
				if (this.isSettingPopup) {
					this.$refs.settingPopup.open()
					this.isSettingPopup = !this.isSettingPopup
				} else {
					this.$refs.settingPopup.close()
					this.isSettingPopup = !this.isSettingPopup
				}
			},
			// 加载下一章
			loadNextChapter() {
				console.log('触发加载下一章逻辑')
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		background-color: #f6f8fa;
	}

	/* 头部导航栏样式 */
	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 24rpx 0;
		background: #fff;
		position: relative;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 1000;

		.nav-back {
			width: 88rpx;
			padding-left: 32rpx;
		}

		.header-icon {
			display: flex;
			padding-right: 20rpx;
		}
	}

	// 阅读内容区
	.content-area {
		flex: 1;
		padding: 20px 16px;
		margin-top: 42px;
		
		::v-deep .uni-scroll-view-content {
			width: 90% !important;
		}

		.chapter-title {
			font-size: 20px;
			font-weight: bold;
			margin-bottom: 16px;
			display: block;
		}

		.content-text {
			font-size: 16px;
			line-height: 1.8;
			color: #333;
		}
	}


	/* 底部工具栏 */
	.toolbar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background: #fff;
		display: flex;
		align-items: center;
		padding: 16rpx 32rpx;
		box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.05);
		z-index: 999;

		.tool {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			color: #666;
		}

		.read-btn {
			background: #f59a23;
			color: #fff;
			padding: 18rpx 58rpx;
			border-radius: 40rpx;
			margin-left: 32rpx;
		}
	}

	// 目录弹窗
	.chapter-container {
		.chapter-popup {
			height: 60vh;

			.popup-header {
				padding: 32rpx;
				border-bottom: 1rpx solid #eee;
				font-size: 32rpx;
				color: #666;
			}

			.chapter-scroll {
				max-height: calc(60vh - 104rpx);
			}

			.chapter-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 28rpx 32rpx;
				border-bottom: 1rpx solid #eee;

				.title {
					font-size: 28rpx;
					color: #333;
				}

				.free-tag {
					color: #f59a23;
					font-size: 24rpx;
					padding: 4rpx 16rpx;
					background: rgba(245, 154, 35, 0.1);
					border-radius: 24rpx;
				}
			}
		}
	}

	// 设置弹窗样式
	.setting-popup {
		padding: 32rpx;
		background: #fff;

		.setting-item {
			margin-bottom: 48rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			// 字号控制
			.font-control {
				display: flex;
				align-items: center;

				.btn {
					color: #666;
					padding: 0 20rpx;
				}

				.value {
					font-size: 36rpx;
					margin: 0 32rpx;
				}
			}

			// 颜色选择
			.color-picker {
				display: flex;
				gap: 24rpx;

				.color {
					width: 64rpx;
					height: 64rpx;
					border-radius: 50%;
					border: 2rpx solid transparent;

					&.active {
						border-color: #f59a23;
					}
				}
			}

			// 翻页模式
			.page-mode {
				display: flex;
				gap: 24rpx;

				.mode-btn {
					padding: 12rpx 32rpx;
					border-radius: 40rpx;
					background: #eee;

					&.active {
						background: #f59a23;
						color: white;
					}
				}
			}
		}
	}
</style>